import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class MyApp20 extends StatefulWidget {
  const MyApp20({super.key});

  @override
  State<MyApp20> createState() => _MyApp20State();
}

class _MyApp20State extends State<MyApp20> {
  // 频道列表
  List channelList = [];

  @override
  void initState() {
    super.initState();
    // 1.获取频道列表
    _getChannelList();
  }

  _getChannelList() async {
    String baseUrl = "https://geek.itheima.net";
    // 1.1  创建Dio实例
    Dio dio = Dio();
    var res = await dio.get('$baseUrl/v1_0/channels');
    print(res.data);
    setState(() {
      channelList = res.data['data']['channels'] as List;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          // widget 获取上边类实例
          title: const Text('Dio初体验'),
        ),
        body: GridView(
            gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 100,
                childAspectRatio: 1.5,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10),
            children: channelList.map((item) {
              return Container(
                alignment: Alignment.center,
                color: Colors.blue,
                child: Text(item['name']),
              );
            }).toList()));
  }
}
